package main

import (
	"bufio"
	"fmt"
	"net"
	"os"
)

func recv(conn *net.UDPConn) {
	msg := make([]byte, 1024)
	for {
		n, udpAddr, err := conn.ReadFromUDP(msg)
		fmt.Println("Response:", string(msg), n, err, udpAddr)

	}

}
func main() {
	//serverAddr := "42.192.199.243:50000"
	addr, _ := net.ResolveUDPAddr("udp", ":6000")
	saddr, _ := net.ResolveUDPAddr("udp", "42.192.199.243:50000")
	conn, err := net.DialUDP("udp", addr, saddr)
	if err != nil {
		fmt.Println("-------error", err)
		return
	}

	defer conn.Close()

	input := bufio.NewScanner(os.Stdin)
	go recv(conn)
	for input.Scan() {
		line := input.Text()

		lineLen := len(line)

		n := 0
		for written := 0; written < lineLen; written += n {
			var toWrite string
			if lineLen-written > 1024 {
				toWrite = line[written:1024]
			} else {
				toWrite = line[written:]
			}

			n, err = conn.Write([]byte(toWrite))

			fmt.Println("Write:", toWrite, n, err)

		}
	}

}
